package com.spring.sunflower.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import java.util.ArrayList;
import java.util.List;

public class RecordWaveView extends View {
  private static final int MAX_ALPHA = 255;
  
  private int centerX;
  
  private int centerY;
  
  private Context context;
  
  private int layoutHeight;
  
  private int layoutWidth;
  
  private List<Circle> mCircleList = new ArrayList();
  
  private Runnable mCreateCircle = new Runnable() {
      public void run() {
        if (RecordWaveView.this.mIsRunning) {
          RecordWaveView.this.newCircle();
          RecordWaveView recordWaveView;
          postDelayed(mCreateCircle, mSpeed);
        } 
      }
    };
  
  private long mDuration = 5000L;
  
  private float mInitialRadius;
  
  private Interpolator mInterpolator = new LinearInterpolator();
  
  private boolean mIsRunning;
  
  private long mLastCreateTime;
  
  private float mMaxRadius;
  
  private float mMaxRadiusRate = 1.0F;
  
  private boolean mMaxRadiusSet;
  
  private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  
  private int mSpeed = 800;
  
  public RecordWaveView(Context paramContext) {
    super(paramContext);
    this.context = paramContext;
    init();
  }
  
  public RecordWaveView(Context paramContext, AttributeSet paramAttributeSet) {
    super(paramContext, paramAttributeSet);
    this.context = paramContext;
    init();
  }
  
  private void init() {
    this.mPaint.setStyle(Paint.Style.FILL);
    this.mPaint.setColor(-1);
  }
  
  private void newCircle() {
    long l = System.currentTimeMillis();
    if (l - this.mLastCreateTime < this.mSpeed)
      return; 
    Circle circle = new Circle();
    this.mCircleList.add(circle);
    invalidate();
    this.mLastCreateTime = l;
  }
  protected void onDraw(Canvas canvas) {
    for (Circle circle : this.mCircleList) {
      float f = circle.getCurrentRadius();
      if (System.currentTimeMillis() - circle.mCreateTime < this.mDuration) {
        this.mPaint.setAlpha(circle.getAlpha());
        int i = this.centerX;
        if (i != 0) {
          int j = this.centerY;
          if (j != 0)
            canvas.drawCircle(i, j, f, this.mPaint);
        } 
        continue;
      }
//      null.remove();
    } 
    if (this.mCircleList.size() > 0)
      postInvalidateDelayed(50L); 
  }
  
  protected void onLayout(boolean paramBoolean, int paramInt1, int paramInt2, int paramInt3, int paramInt4) {
    super.onLayout(paramBoolean, paramInt1, paramInt2, paramInt3, paramInt4);
    paramInt1 = paramInt3 - paramInt1;
    this.layoutWidth = paramInt1;
    paramInt2 = paramInt4 - paramInt2;
    this.layoutHeight = paramInt2;
    this.centerX = paramInt1 / 2;
    this.centerY = paramInt2 / 2;
  }
  
  protected void onSizeChanged(int paramInt1, int paramInt2, int paramInt3, int paramInt4) {
    if (!this.mMaxRadiusSet)
      this.mMaxRadius = Math.min(paramInt1, paramInt2) * this.mMaxRadiusRate / 2.0F; 
  }
  
  public void setDuration(long paramLong) { this.mDuration = paramLong; }
  
  public void setInitialRadius(float paramFloat) { this.mInitialRadius = paramFloat; }
  
  public void setInterpolator(Interpolator paramInterpolator) {
    this.mInterpolator = paramInterpolator;
    if (paramInterpolator == null)
      this.mInterpolator = new LinearInterpolator(); 
  }
  
  public void setMaxRadius(float paramFloat) {
    this.mMaxRadius = paramFloat;
    this.mMaxRadiusSet = true;
  }
  
  public void setMaxRadiusRate(float paramFloat) { this.mMaxRadiusRate = paramFloat; }
  
  public void setPaintColor(int paramInt) { this.mPaint.setColor(paramInt); }
  
  public void setSpeed(int paramInt) { this.mSpeed = paramInt; }
  
  public void start() {
    if (!this.mIsRunning) {
      this.mIsRunning = true;
      this.mCreateCircle.run();
    } 
  }
  
  public void stop() { this.mIsRunning = false; }
  
  public void stopImmediately() {
    this.mIsRunning = false;
    this.mCircleList.clear();
    invalidate();
  }
  
  private class Circle {
    private long mCreateTime = System.currentTimeMillis();
    
    int getAlpha() {
      float f = (getCurrentRadius() - RecordWaveView.this.mInitialRadius) / (RecordWaveView.this.mMaxRadius - RecordWaveView.this.mInitialRadius);
      return (int)(255.0F - RecordWaveView.this.mInterpolator.getInterpolation(f) * 255.0F);
    }
    
    float getCurrentRadius() {
      float f = (float)(System.currentTimeMillis() - this.mCreateTime) * 1.0F / (float)RecordWaveView.this.mDuration;
      return RecordWaveView.this.mInitialRadius + RecordWaveView.this.mInterpolator.getInterpolation(f) * (RecordWaveView.this.mMaxRadius - RecordWaveView.this.mInitialRadius);
    }
  }
}